package com.rpframework.website.device.act.api;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.rpframework.core.BaseAct;
import com.rpframework.utils.NumberUtils;
import com.rpframework.utils.Pager;
import com.rpframework.website.device.domain.Goods;
import com.rpframework.website.device.domain.GoodsStock;
import com.rpframework.website.device.domain.TCollect;
import com.rpframework.website.device.domain.User;
import com.rpframework.website.device.exception.APICodeException;
import com.rpframework.website.device.service.CollectService;
import com.rpframework.website.device.service.EstateService;
import com.rpframework.website.device.service.GoodsService;
import com.rpframework.website.device.service.GoodsStockService;

/**
 * 商品模块接口
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/api/goods")
public class GoodsApiAct extends BaseAct {
	@Resource GoodsService service;
	@Resource EstateService estateService;
	@Resource GoodsStockService goodsStockService;
	@Resource CollectService collectService;
	
	
	@RequestMapping(value="/list",produces = "application/json; charset=utf-8")
	public @ResponseBody String listone(@RequestParam(value="pager",required = false ) Pager<Goods> pager,
			@RequestParam(value="category",required = false ) String category,/*1.冷鲜食品  2.休闲食品3.奶酒饮料4.粮油副食5.清洁用品6.个人护理7.生活日用8.母婴用品9.其它*/
			@RequestParam(value="label",required = false ) String label,/*标签 1.热门推荐 2.精品推荐 3.促销专区 4.热卖榜单*/  
			@RequestParam(value="state",required = false ) String state,/*状态 1.正常 2.禁用*/
			@RequestParam(value="name",required = false ) String name,/*状态 1.正常 2.禁用*/
			@RequestParam(value = "callback",required = false ) String callback,
			HttpSession session){
		if(pager==null){
			pager=new Pager<Goods>();
		}
		if(StringUtils.isNotBlank(category))pager.getSearchMap().put("category", category);
		if(StringUtils.isNotBlank(label))pager.getSearchMap().put("label", label);
		if(StringUtils.isNotBlank(state))pager.getSearchMap().put("state", state);
		if(StringUtils.isNotBlank(name))pager.getSearchMap().put("name", name);
		pager = service.getPager(pager);
		JsonObject json=new JsonObject();
		json.addProperty("totalPages", pager.getTotalPages());
		json.addProperty("currentPage", pager.getCurrentPage());
		json.addProperty("totalCount", pager.getTotalCount());
		List<Goods> list=pager.getItemList();
		if(list!=null && list.size()>0){
		JsonArray array=new JsonArray();
			for(Goods t : list){
				JsonObject obj = new JsonObject();
				obj.addProperty("id", t.getId());	
				JsonArray arr = new JsonParser().parse(t.getUrlList()).getAsJsonArray();
				obj.add("urlList", arr);//商品图片
				obj.addProperty("name", t.getName());//商品名字
				obj.addProperty("disPrice", t.getDisPrice());//商品折扣价		
				obj.addProperty("oldPrice", t.getOldPrice());//商品原价
				array.add(obj);
			}
			json.add("arrays", array);
		}
		return (callback + "(" + json + ")");
	}	
	/**
	 * 商品详情 
	 * @param id
	 * @param callback
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/detail",produces = "application/json; charset=utf-8")
	public @ResponseBody String Detail(@RequestParam(value="id",required = false ) Integer id,
			@RequestParam(value = "callback",required = false ) String callback,
			HttpSession session){
		JsonObject json = new JsonObject();
		User user = getSessionUser(session);
		if(user==null){
			throw new APICodeException(-4, "您尚未登录!");
		}
		Goods t = service.select(id);
		JsonArray arr = new JsonParser().parse(t.getUrlList()).getAsJsonArray();
		json.add("urlList", arr);
		json.addProperty("name", t.getName());
		json.addProperty("disPrice", t.getDisPrice());
		json.addProperty("oldPrice", t.getOldPrice());
		json.addProperty("sales", t.getSales());
		json.addProperty("point", t.getPoint());
		json.addProperty("category", t.getCategory());
		json.addProperty("label", t.getLabel());
		/*查看当前用户所属物业是否有货，库存多少，如果没有就取平台的库存*/
		GoodsStock gs = goodsStockService.searchStock(t.getId(), user.getEstateId());
		if(gs==null ||gs.getAmount()<1){//如果物业缺货 直接查平台的 并通过relativeId可以判断供货的对象是平台还是物业
			gs = goodsStockService.searchStock(t.getId(), 0);
		}
		GoodsStock pt = goodsStockService.searchStock(t.getId(), 0);//平台该商品的库存
		Double allstock = 0d;
		if(pt!=null)allstock += pt.getAmount();
		if(gs!=null){ //物业 总数
			json.addProperty("supplier", gs.getRelativeId()==0 ? "平台" : "物业");
			json.addProperty("stock",gs.getAmount());
			allstock += gs.getAmount();
		}else{
			json.addProperty("supplier","平台");
			json.addProperty("stock",0);
		}
		/*当前登录用户的收藏表里是否有这条数据*/
		TCollect c = collectService.getCollectByUserGoods(user.getId(), t.getId());
		if(c!=null && NumberUtils.isValid(c.getId())){
			json.addProperty("isCollect", c.getState());//1表示收藏
		}else{
			json.addProperty("isCollect", 2);//2表示未收藏
		}
		json.addProperty("allstock", allstock);
		json.addProperty("info", t.getProduction());
		json.add("remark", service.getJsonInfo());
		return (callback + "(" + json + ")");
	}
	
	//社区活动类型 数据字典map
	@RequestMapping(value="/dict",produces = "application/json; charset=utf-8")
	public @ResponseBody String getDataDict(
			@RequestParam(value = "callback",required = false ) String callback,
			@RequestParam(value="type",required=false) String type){
		JsonObject json = new JsonObject();
		if(StringUtils.isBlank(type)){
			json.addProperty("succ", false);
			json.addProperty("msg", "参数列表必须带有:"+"type");
			return (callback + "(" + json + ")");
		}
		json = service.getDataDictByType(type);
		return (callback + "(" + json + ")");
	}
}
